from kubernetes import client, config
class k8sCoreV1Api:
    def __init__(self,config_file):
        self.config_file=config_file
        self.config = config.kube_config.load_kube_config(config_file=self.config_file)
        self.v1 = client.CoreV1Api()

    def list_namespace(self):
        namespaces = []
        for ns in self.v1.list_namespace().items:
            namespaces.append(ns.metadata.name)
        return namespaces

    def list_nodes(self):
        nodes = self.v1.list_node().items
        list_nodes = []
        for node in nodes:
            nodeinfo = {}
            for addres in node.status.addresses:
                if addres.type == 'Hostname':
                    nodeinfo['Hostname'] = addres.address
                if addres.type == 'InternalIP':
                    nodeinfo['InternalIP'] = addres.address
            nodeinfo['cpu'] = node.status.allocatable['cpu']
            nodeinfo['memory'] = node.status.allocatable['memory']
            list_nodes.append(nodeinfo)
        return list_nodes

class k8sAppsV1Api:
    def __init__(self,config_file):
        self.config_file=config_file
        self.config = config.kube_config.load_kube_config(config_file=self.config_file)
        self.v1 = client.AppsV1Api()

    def list_deployment(self):
        deployments = self.v1.list_deployment_for_all_namespaces().items
        list_deployments = []
        for deploy in deployments:
            deploymentsinfo = {}
            namespace = deploy.metadata.namespace
            name = deploy.metadata.name
            deploymentsinfo['namespace'] = namespace
            deploymentsinfo['name'] = name
            list_deployments.append(deploymentsinfo)
        return list_deployments

 # if __name__ == '__main__':
 #     config_file = 'kubeconfig.yaml'
 #     test = k8sAppsV1Api(config_file)
 #     test1 = k8sCoreV1Api(config_file)
#     list_deploment = test.list_deployment()
#     list_node = test1.list_nodes()
#     #print(list_deploment)
#     print(type(list_node))
#     for i in list_node:
#         print(i)
#         for k,v in i.items():
#             print(k+":"+v)
#         print(type(i))
#     print(list_node)